// pages/user/concat/concat.js
const md5 = require('../../../utils/md5.js')
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        concatImg: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.getConcatImg()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    getConcatImg: function() {
        const that = this
        let data = {}
        common.post('User/get_contact', data).then(function(res) {
            that.setData({
                concatImg: res
            })
        }).catch(function(e) {})
    },

    objKeySort: function(obj) {
        let newkey = Object.keys(obj).sort()
        let newObj = {}
        for (let i = 0; i < newkey.length; i++) {
            newObj[newkey[i]] = obj[newkey[i]]
        }
        return newObj
    },

    uploadImg: function() {
        const that = this
        let data = {}
        let key = wx.getStorageSync('token')
        let sign = 'mozi'
        let timestamp = Date.parse(new Date())
        data.key = key
        data.timestamp = timestamp
        data = this.objKeySort(data)
        for (let i in data) {
            sign += (i + data[i])
        }
        sign += 'mozi'
        sign = md5(sign)
        data.sign = sign
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function(res) {
                wx.showLoading({
                    title: '正在上传图片',
                })
                const tempFilePaths = res.tempFilePaths
                wx.uploadFile({
                    url: 'https://mozi.bailingyoupin.com/api/Upimg/up_comment_img', //仅为示例，非真实的接口地址
                    filePath: tempFilePaths[0],
                    name: 'file',
                    formData: {
                        key: data.key,
                        timestamp: data.timestamp,
                        sign: data.sign
                    },
                    success(res) {
                        that.setData({
                            concatImg: JSON.parse(res.data).datas
                        })
                        let datas = {
                            user_contact: that.data.concatImg
                        }
                        common.post('User/up_user_contact', datas).then(function(res) {
                            wx.hideLoading()
                            wx.showToast({
                                title: '上传图片成功',
                                icon: 'none',
                                duration: 1000
                            })
                        }).catch(function(e) {})
                    }
                })
            }
        })
    }
})